package com.zzyl.nursing.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.HashMap;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zzyl.common.core.page.TableDataInfo;
import com.zzyl.nursing.dto.ElderPageQuery;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.zzyl.nursing.mapper.ElderMapper;
import com.zzyl.nursing.domain.Elder;
import com.zzyl.nursing.service.IElderService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;

/**
 * 老人Service业务层处理
 *
 * @Author: Zhy
 * @Date: 2025-02-25
 */
@Service
@RequiredArgsConstructor
public class ElderServiceImpl extends ServiceImpl<ElderMapper, Elder> implements IElderService {

	private final ElderMapper elderMapper;

	/**
	 * 查询老人
	 *
	 * @param id 老人主键
	 * @return 老人
	 */
	@Override
	public Elder selectElderById(Long id) {
		return elderMapper.selectById(id);
	}

	/**
	 * 查询老人列表
	 *
	 * @param elder 老人
	 * @return 老人
	 */
	@Override
	public List<Elder> selectElderList(Elder elder) {
		return elderMapper.selectElderList(elder);
	}

	/**
	 * 新增老人
	 *
	 * @param elder 老人
	 * @return 结果
	 */
	@Override
	public int insertElder(Elder elder) {
		return elderMapper.insert(elder);
	}

	/**
	 * 修改老人
	 *
	 * @param elder 老人
	 * @return 结果
	 */
	@Override
	public int updateElder(Elder elder) {
		return elderMapper.updateById(elder);
	}

	/**
	 * 批量删除老人
	 *
	 * @param ids 需要删除的老人主键
	 * @return 结果
	 */
	@Override
	public int deleteElderByIds(Long[] ids) {
		return elderMapper.deleteBatchIds(Arrays.asList(ids));
	}

	/**
	 * 删除老人信息
	 *
	 * @param id 老人主键
	 * @return 结果
	 */
	@Override
	public int deleteElderById(Long id) {
		return elderMapper.deleteById(id);
	}

	/**
	 * 查询已经入住的老人列表
	 *
	 * @return:
	 * @param:
	 */
	@Override
	public TableDataInfo<Elder> pageQuery(ElderPageQuery elderPageQuery) {
		// 只返回有有效入住记录的老人（check_in.status=0，且end_date为空或大于当前日期）
		Map<String, Object> params = new HashMap<>();
		params.put("name", elderPageQuery.getName());
		params.put("idCardNo", elderPageQuery.getIdCardNo());
		params.put("pageSize", elderPageQuery.getPageSize());
		int offset = (elderPageQuery.getPageNum() - 1) * elderPageQuery.getPageSize();
		params.put("offset", offset);
		List<Elder> elderList = elderMapper.selectElderWithValidCheckIn(params);
		int total = elderMapper.countElderWithValidCheckIn(params);
		return new TableDataInfo(elderList, total);
	}

	@Override
	public List<Elder> selectElderListByNameAndStatus(String name, Integer status) {
		QueryWrapper<Elder> wrapper = new QueryWrapper<>();
		if (name != null && !name.isEmpty()) {
			wrapper.like("name", name);
		}
		if (status != null) {
			wrapper.eq("status", status);
		}
		return this.list(wrapper);
	}
}
